package cn.kimming.wms.domain;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import cn.kimming.wms.util.FileUploadUtils;
import lombok.Getter;
import lombok.Setter;

//商品
public class Product extends BaseDomain{
	@Getter @Setter
	private String sn;	//编码
	@Getter @Setter
	private String name;	//名称
	@Getter @Setter
	private BigDecimal costPrice;	//成本价
	@Getter @Setter
	private BigDecimal salePrice;	//销售价
	@Getter @Setter
	private String imagePath;	//图片路径
	@Getter @Setter
	private String intro;	//商品简介
	
	@Getter @Setter
	private Brand brand;	//对应品牌:many2one, brand_id

	@Override
	public String toString() {
		return "Product [sn=" + sn + ", name=" + name + ", costPrice=" + costPrice + ", salePrice=" + salePrice
				+ ", imagePath=" + imagePath + ", intro=" + intro + ", brand=" + brand + ", id=" + id + "]";
	}	

	//返回图片的缩略图路径
	public String getSmallImagePath() {
		if (this.imagePath == null) {
			return "";
		}
		int index = this.imagePath.lastIndexOf(".");
		return this.imagePath.substring(0, index) + FileUploadUtils.suffix + this.imagePath.substring(index);
	}
	
	//返回添加订单明细时货品的JSON数据格式
	public String getProductJson() {
		Map<String, Object> map = new HashMap<>();
		map.put("id", id);
		map.put("name", name);
		map.put("brandName", brand == null ? "暂无品牌" : brand.getName());
		map.put("costPrice", costPrice);
		map.put("salePrice", salePrice);
		return JSON.toJSONString(map);
	}
	
	
}
